local dap, dapui = require "dap", require "dapui"

-- keys = {
--   {"<leader>db", function() require("dap").toggle_breakpoint() end, desc = "toggle [d]ebug [b]reakpoint" },
--   {"<leader>dB", function() require("dap").set_breakpoint(vim.fn.input("Breakpoint condition: ")) end, desc = "[d]ebug [B]reakpoint"},
--   {"<leader>dc", function() require("dap").continue() end, desc = "[d]ebug [c]ontinue (start here)" },
--   {"<leader>dC", function() require("dap").run_to_cursor() end, desc = "[d]ebug [C]ursor" },
--   {"<leader>dg", function() require("dap").goto_() end, desc = "[d]ebug [g]o to line" },
--   {"<leader>do", function() require("dap").step_over() end, desc = "[d]ebug step [o]ver" },
--   {"<leader>dO", function() require("dap").step_out() end, desc = "[d]ebug step [O]ut" },
--   {"<leader>di", function() require("dap").step_into() end, desc = "[d]ebug [i]nto" },
--   {"<leader>dj", function() require("dap").down() end, desc = "[d]ebug [j]ump down" },
--   {"<leader>dk", function() require("dap").up() end, desc = "[d]ebug [k]ump up" },
--   {"<leader>dl", function() require("dap").run_last() end, desc = "[d]ebug [l]ast" },
--   {"<leader>dp", function() require("dap").pause() end, desc = "[d]ebug [p]ause" },
--   {"<leader>dr", function() require("dap").repl.toggle() end, desc = "[d]ebug [r]epl" },
--   {"<leader>dR", function() require("dap").clear_breakpoints() end, desc = "[d]ebug [R]emove breakpoints" },
--   {"<leader>ds", function() require("dap").session() end, desc ="[d]ebug [s]ession" },
--   {"<leader>dt", function() require("dap").terminate() end, desc = "[d]ebug [t]erminate" },
--   {"<leader>dw", function() require("dap.ui.widgets").hover() end, desc = "[d]ebug [w]idgets" },
-- },

-- dap

vim.keymap.set('n', '<S-F5>', function() require('dap').terminate() end, {desc="dap stop"})
vim.keymap.set('n', '<C-F5>', function() require('dap').run_last() end, {desc='dap run last'})
vim.keymap.set('n', '<F6>', function() require('dap').step_over() end, {desc='dap 跳过'})
vim.keymap.set('n', '<F7>', function() require('dap').step_into() end, {desc='dap 跳入'})
vim.keymap.set('n', '<F8>', function() require('dap').step_out() end, {desc='dap 跳出'})

vim.keymap.set('n', '<Leader>[b', function() require('dap').toggle_breakpoint() end, {desc='dap 开关断点'})
vim.keymap.set('n', '<Leader>[B', function() require('dap').set_breakpoint() end, {desc='dap 设置断点'})
vim.keymap.set('n', '<Leader>[p', function() require('dap').set_breakpoint(nil, nil, vim.fn.input('Log point message: ')) end)
vim.keymap.set('n', '<Leader>[r', function() require('dap').repl.toggle() end, {desc='dap 开关repl'})
vim.keymap.set('n', '<Leader>[[', function() require('dapui').close() end, {desc='dapui close'})


vim.keymap.set({'n', 'v'}, '<Leader>[h', function()
  require('dap.ui.widgets').hover()
end)
vim.keymap.set({'n', 'v'}, '<Leader>[p', function()
  require('dap.ui.widgets').preview()
end)
vim.keymap.set('n', '<Leader>[f', function()
  local widgets = require('dap.ui.widgets')
  widgets.centered_float(widgets.frames)
end)
vim.keymap.set('n', '<Leader>[s', function()
  local widgets = require('dap.ui.widgets')
  widgets.centered_float(widgets.scopes)
end)


dap.adapters.lldb = {
  type = 'executable',
  command = 'lldb-dap',
  name = 'lldb'
}

dap.configurations.cpp = {
  -- [configurations](https://github.com/llvm/llvm-project/tree/main/lldb/tools/lldb-dap#configuration-settings-reference)
  {
    name = 'Launch',
    type = 'lldb',
    request = 'launch',
    program = function()
      return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
    end,
    cwd = '${workspaceFolder}',
    stopOnEntry = false,
    args = {},

    -- 💀
    -- if you change `runInTerminal` to true, you might need to change the yama/ptrace_scope setting:
    --
    --    echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
    --
    -- Otherwise you might get the following error:
    --
    --    Error on launch: Failed to attach to the target process
    --
    -- But you should be aware of the implications:
    -- https://www.kernel.org/doc/html/latest/admin-guide/LSM/Yama.html
    -- runInTerminal = false,
  },
}

dap.configurations.c = dap.configurations.cpp
dap.configurations.rust = dap.configurations.cpp

-- dapui config
--
dap.listeners.before.attach.dapui_config = function()
  dapui.open()
end
dap.listeners.before.launch.dapui_config = function()
  dapui.open()
end
dap.listeners.before.event_terminated.dapui_config = function()
  dapui.close()
end
dap.listeners.before.event_exited.dapui_config = function()
  dapui.close()
end
